package com.slimerevenge.model;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.slimerevenge.utilities.MapBodyReader;
import com.slimerevenge.view.WorldRenderer;

public class Level {
	/**
	 * Array of blocks instances 
	 */
	private Array<Body> mapObjArr = new Array<Body>();
	private Array<Block> blocks = new Array<Block>();
	private TiledMap map;
	/**
	 * Player Instance 
	 */
	private Player player;
	
	/**
	 * This is the main box2d "container" object. All bodies will be loaded in
	 * this object and will be simulated through calls to this object.
	 */
	private World world;
	private FileHandle tmxName = null;
	
	//Gravity for the world (Likely to apply for all objects in current level)
	private Vector2 gravity = new Vector2(0.0f,-15.0f);
	
	// Getters -----------
	public Array<Block> getBlocks() {
		return blocks;
	}

	public Array<Body> getMapObjArray() {
		return mapObjArr;
	}

	public Player getPlayer() {
		return player;
	}
	
	public TiledMap getMap() {
		return map;
	}
	
	public World getWorld(){
		return world;
	}
	
	public FileHandle getMapFileName(){
		return tmxName;
	}

	// --------------------

	public Level() {
		renderWorld("level/test.tmx");
	}

	private void renderWorld(String levelPath) {
		
		tmxName = Gdx.files.internal(levelPath);
		
		map = new TmxMapLoader().load(tmxName.path());
		
		world = new World(gravity, true);
		
		float playerpos_x = Float.parseFloat((String)map.getProperties().get("playerxpos"));
		float playerpos_y = Float.parseFloat((String)map.getProperties().get("playerypos"));
		
		player = new Player(new Vector2(playerpos_x,playerpos_y), world);
		
		mapObjArr = (MapBodyReader.buildShapes(map, WorldRenderer.PIXELS_PER_TILE, world));
		

		//worldrend = ;
		
		Gdx.input.setInputProcessor(player.getGestureDetector());
		/*for (int i = 0; i < 15; i++) {
			blocks.add(new Block(new Vector2(i, 0),world));
			if(i<6)
				blocks.add(new Block(new Vector2(i, 1),world));
		}
		blocks.add(new Block(new Vector2(1, 3.5f),world));
		blocks.add(new Block(new Vector2(2, 3.5f),world));
		blocks.add(new Block(new Vector2(5, 4),world));
		blocks.add(new Block(new Vector2(6, 4),world));
		
		blocks.add(new Block(new Vector2(-5, 9),world));
		blocks.add(new Block(new Vector2(-6, 9),world));

		//world.setContactListener(new WorldRenderer(this,false));*/
	}
	
	
}
